package com.linn.home.controller;

import com.github.pagehelper.PageInfo;
import com.linn.frame.controller.BaseController;
import com.linn.home.entity.Node;
import com.linn.frame.entity.ResultBean;
import com.linn.frame.entity.ResultTable;
import com.linn.frame.util.SysContent;
import com.linn.home.entity.Role;
import com.linn.home.entity.RoleMenu;
import com.linn.home.service.RoleMenuService;
import com.linn.home.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 角色controller
 * Created by Administrator on 2018/8/9.
 */
@Controller
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    /**
     * 跳转到角色列表
     * @return
     */
    @RequestMapping("/admin/toRoleList")
    private String toRoleList() {
        return "admin/roleList";
    }

    /**
     * 获取json数据
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/getRoleListDate")
    public ResultTable getRoleListDate(@RequestParam(value = "page") Integer page,
                                           @RequestParam(value = "limit") Integer limit) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(limit);
        pageInfo = roleService.findRoleList(pageInfo);
        return new ResultTable(String.valueOf(SysContent.SUCCESS), "", pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 弹出角色form
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/admin/toRoleForm")
    public String toRoleForm(@RequestParam(value = "id", required = false) Integer id ,Model model) {
        if (!StringUtils.isEmpty(id)) {
            Role role = roleService.findRoleById(id);
            model.addAttribute("role", role);
        }
        return "admin/roleForm";
    }

    /**
     * 添加/更新菜单
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/addOrUpdateRole")
    private ResultBean addOrUpdateRole(Role role,@RequestParam("menuIdArr") String menuIdArr){
        String[] menuIds = null;
        if (!StringUtils.isEmpty(menuIdArr)){
            menuIds = menuIdArr.split(",");
        }

        if (StringUtils.isEmpty(role.getId())) {
            //添加角色
            role.setGmtCreate(new Date());
            role.setGmtModified(new Date());
            int ret = roleService.addRole(role,menuIds);
        } else {
            //更新
            role.setGmtModified(new Date());
            int ret = roleService.updateRole(role,menuIds);
        }

        return new ResultBean(SysContent.SUCCESS, "成功");
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/delRole")
    private ResultBean delRole(Role role){
        if (StringUtils.isEmpty(role.getId())) {
            return new ResultBean(SysContent.ERROR, "id不能为空");
        } else {
            int ret = roleService.deleteRole(role);
        }
        return new ResultBean(SysContent.SUCCESS, "成功");
    }

    /**
     * 批量删除
     * @param roleIds
     * @return
     */
    @RequestMapping("admin/batchRemoveRole")
    @ResponseBody
    private ResultBean batchRemoveRole(@RequestParam("ids[]") int[] roleIds) {

        int r = roleService.batchRemoveRole(roleIds);
        return new ResultBean(SysContent.SUCCESS, "成功");
    }

    /**
     *通过角色ID查询角色下有哪些菜单信息
     * @param roleId
     * @return
     */
    @RequestMapping("admin/showRoleMenu")
    @ResponseBody
    private ResultBean showRoleMenu(@RequestParam("roleId") Integer roleId){

        List<Node> nodes = roleService.findRoleMenuList(roleId);
        return new ResultBean(SysContent.SUCCESS, "成功",nodes);
    }

}
